package org.csu.mypetstore.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.domain.PO.*;
import org.csu.mypetstore.domain.BO.AccountBO;
import org.csu.mypetstore.mapper.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.utils.Md5Util;
import org.csu.mypetstore.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SignonMapper signonMapper;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private BannerDdataMapper bannerDdataMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AccountmessageMapper accountmessageMapper;

    @Override
    public Account getAccountByUsername(String username) {
        //查询account表并返回该用户
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("username",username);
        Account account = accountMapper.selectOne(accountQueryWrapper);
        if(account != null)
            return account;
        return null;
    }

    public String getToken(String username, String password) {
        //通过用户名密码返回token
        QueryWrapper<Signon> signonQueryWrapper = new QueryWrapper<>();
        signonQueryWrapper.eq("username",username).eq("password",Md5Util.md5(password));
        if(signonMapper.selectOne(signonQueryWrapper) != null)
            return TokenUtils.token(username);//登录成功返回一个token信息
        return null;
    }

    @Override
    public boolean isUsernameExist(String username) {
        return getAccountByUsername(username) != null;
    }

    public Account insertAccount(AccountBO accountBO){
        Account account = getAccountFromAccountBO(accountBO);
        Signon signon = getSionFromAccountBO(accountBO);
        accountMapper.insert(account);
        signonMapper.insert(signon);
        return account;
    }

    public Account updateAccount(AccountBO accountBO) {
        Account account = getAccountFromAccountBO(accountBO);
        Signon signon = getSionFromAccountBO(accountBO);
        UpdateWrapper<Account> accountVOUpdateWrapper = new UpdateWrapper<>();
        accountVOUpdateWrapper.eq("username", account.getUsername());
        accountMapper.update(account, accountVOUpdateWrapper);
        UpdateWrapper<Signon> signonUpdateWrapper = new UpdateWrapper<>();
        signonUpdateWrapper.eq("username", signon.getUsername());
        signonMapper.update(signon, signonUpdateWrapper);
        return account;
    }

    @Override
    public Account cancelAccount(Account account) {
        //先找到account对应的密码表
        QueryWrapper<Signon> signonQueryWrapper = new QueryWrapper<>();
        signonQueryWrapper.eq("username",account.getUsername());
        Signon signon = signonMapper.selectOne(signonQueryWrapper);
        signon.setAlive(false);//设置为死号
        //更新对应的密码表
        UpdateWrapper<Signon> signonUpdateWrapper = new UpdateWrapper<>();
        signonUpdateWrapper.eq("username", signon.getUsername());
        signonMapper.update(signon, signonUpdateWrapper);
        return account;
    }

    public static Account getAccountFromAccountBO(AccountBO accountBO){
        Account account = new Account();
        account.setUsername(accountBO.getUsername());
        account.setEmail(accountBO.getEmail());
        account.setFirstname(accountBO.getFirstname());
        account.setLastname(accountBO.getLastname());
        account.setAddress1(accountBO.getAddress1());
        account.setAddress2(accountBO.getAddress2());
        account.setCity(accountBO.getCity());
        account.setProvince(accountBO.getProvince());
        account.setZip(accountBO.getZip());
        account.setCountry(accountBO.getCountry());
        account.setPhone(accountBO.getPhone());
        return account;
    }

    public static Signon getSionFromAccountBO(AccountBO accountBO){
        Signon signon = new Signon();
        signon.setUsername(accountBO.getUsername());
        signon.setPassword(Md5Util.md5(accountBO.getPassword()));
        signon.setAlive(true);
        return signon;
    }

    //通过用户名和密钥查找管理员信息
//    public CommonResponse<Admin> getAdminByAdminnameAndKeyword(String adminname, String keyword){
//        //通过用户名和密码在管理员表中找到对应信息
//        QueryWrapper<Admin>adminQueryWrapper = new QueryWrapper<>();
//        adminQueryWrapper.eq("adminname",adminname);
//        adminQueryWrapper.eq("keyword",keyword);
//        Admin admin = adminMapper.selectOne(adminQueryWrapper);
//
//        //如果获得的admin对象不为空，设置account中的信息
////        if(admin != null){
////            admin.setAccount(getAccount(adminname));
////        }
//        return CommonResponse.createForSuccess(admin);
//    }

    //获取用户列表
//    public List<AccountVO> getAccountList(){
//        List<AccountVO> accountVOList = new ArrayList<>();
//        //获取所有用户列表
//        List<Account> accountList = accountMapper.selectList(null);
//
//        //为accoutnlist插入account对象
//        Iterator<Account>iterator = accountList.iterator();
//        while(iterator.hasNext()){
//            Account account = iterator.next();
//            //设置accountVO对象
//            String username = account.getUsername();
//            AccountVO accountVO = getAccount(username);
//            accountVOList.add(accountVO);
//        }
//        return accountVOList;
//    }

//    public void deleteAccountByUsername(String username){
//        QueryWrapper<Signon>signonQueryWrapper = new QueryWrapper<>();
//        signonQueryWrapper.eq("username",username);
//        signonMapper.delete(signonQueryWrapper);
//
//        QueryWrapper<Profile>profileQueryWrapper = new QueryWrapper<>();
//        profileQueryWrapper.eq("userid",username);
//        profileMapper.delete(profileQueryWrapper);
//
//        QueryWrapper<Account>accountVOQueryWrapper = new QueryWrapper<>();
//        accountVOQueryWrapper.eq("userid",username);
//        accountMapper.delete(accountVOQueryWrapper);
//    }
}
